// pages/my/addAddress/addAddress.js
const App = getApp();
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 省市区三级联动初始化
    region: ["浙江省", "杭州市", "滨江区"],
    regionCode: ["330000","330100","330108"],
    phone:'',
    name:'',
    address:'',
  },
  // 选择省市区函数
  changeRegin(e){
    console.log(e.detail.code)
    this.setData({
       region: e.detail.value ,
       regionCode:e.detail.code
      });
  },
  //保存并返回
  seve:function (){
    wx.navigateBack({
      delta: 1
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  //新增地址
  addAddress:function (){
    let phone = this.data.phone;
    let name = this.data.name;
    let address = this.data.address;
    let local1 = this.data.regionCode[0];
    let local2 = this.data.regionCode[1];
    let local3 = this.data.regionCode[2];
    if (name == '') {
      utils.toast('收货人不能为空')
      return
    }
    if (!utils.phoneNumberVer(phone)) {
      return
    }
    if (address == '') {
      utils.toast('详细地址不能为空')
      return
    }
    utils.requestUrl({
      url:'/niu/apiv3/fahuo_address_add.php',
      params:{
        markid:wx.getStorageSync('markId'),
        type:1,
        phone:phone,
        address:address,
        adid:'',
        remark:'add',
        local1:local1,
        local2:local2,
        local3:local3,
        name:name
      }
    }).then((res) =>{
      // let data = res.data
      console.log(res.data);
      if (res.data.resultcode == 0) {
        wx.navigateBack({
          delta: 1
        })
      }else{
        utils.toast(res.data.msg)
      }
    })
  },
  getName:function(e){
		this.setData({
      name: e.detail.value
    })
  },
  getPhone:function(e){
		this.setData({
      phone: e.detail.value
    })
  },
  getAddress:function(e){
		this.setData({
      address: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(wx.getStorageSync('markId') == ''){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})